package logic

import (
	"context"
	"fmt"
	"shop/models/mysql"

	"shop/shoprpc/internal/svc"
	"shop/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginShopLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLonginShopLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginShopLogic {
	return &LonginShopLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LonginShopLogic) LonginShop(in *shoprpc.LonginRequest) (*shoprpc.LonginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	mysql.DB.Debug().Where("account =?", in.Account).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("账号不存在,请前去注册")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码不对")
	}
	return &shoprpc.LonginResponse{
		UserId: int64(user.ID),
	}, nil
}
